/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sudolink.isp.cryptodata;

import com.sudolink.cryptodata.CryptInfoBundle;
import com.sudolink.cryptodata.ISecureData;
import com.sudolink.isp.app.Application;
import java.util.Date;

/**
 *
 * @author matsu
 */
public class SecureDataHelpers {
    
    public static boolean isPasswordExpired(ISecureData data) {

        boolean isPasswordExpired = false;
        boolean isReminderEnabled = SecureDataHelpers.getBoolean(
                data, ISecureData.PASSWORD_REMINDER_ENABLED, true);

        if (isReminderEnabled) {

            long now = new Date().getTime();
            long reset = SecureDataHelpers.getLong(
                    data, ISecureData.PASSWORD_EXPIRE_TIMESPAN_MS, 0);
            long then = SecureDataHelpers.getLong(
                    data, ISecureData.LAST_PASSWORD_RESET_TIME, 0);
            if ((now - then) > reset) {
                isPasswordExpired = true;
            }
        }

        return isPasswordExpired;

    }
    
    
    public static boolean getBoolean( ISecureData isd, String key, boolean _default ) {
        CryptInfoBundle bundle = Application.getPrivateKeys();
        String result = isd.get(key, bundle);
        boolean r;
        if( result != null && !result.isEmpty() ) {
            r = Boolean.parseBoolean(result);
        } else {
            r = _default;
        }
        return r;
    }
    
    public static long getLong( ISecureData isd, String key, long _default ) {
        CryptInfoBundle bundle = Application.getPrivateKeys();
        String value = isd.get(key, bundle);
        long r;
        if( value != null && !value.isEmpty() ) {
            r = Long.parseLong(value);
        } else {
            r = _default;
        }
        return r;
    }
}
